﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using dotNetMembership = System.Web.Security.Membership;
using dotNetRoles = System.Web.Security.Roles;
using Binding = System.Web.Mvc.BindingHelperExtensions;
using StarterKits.Mvc.Filters.FormsAuthentication;
using StarterKits.Mvc.Membership.Controllers;
using P.InOut.NetMvc.Logging;
using P.InOut.NetMvc.Properties;
using P.InOut.NetMvc.Common;
using P.InOut.NetMvc.Models;

namespace P.InOut.NetMvc.Controllers
{
    public class QuotesController : Controller
    {
        private InOutDataContext _db;

        public QuotesController()
        {
            _db = new InOutDataContext();
        }

        ~QuotesController()
        {
            _db.Dispose();
        }

        public ActionResult Index()
        {
            try
            {
                List<Quote> quotes = (from q in _db.Quotes
                              where q.Approved == true
                              select q).ToList<Quote>();

                return View("Index", quotes);
            }
            catch
            {
                return RedirectToAction("Error", "Updates");
            }
        }

        public ActionResult Approve()
        {
            try
            {
                return RedirectToAction("Error", "Updates");
            }
            catch
            {
                return RedirectToAction("Error", "Updates");
            }
        }

        public ActionResult Create()
        {
            try
            {
                using (InOutDataContext db = new InOutDataContext())
                {
                    MembershipUser user = Membership.GetUser();
                    Quote quote = new Quote
                    {
                        UserId = (Guid)user.ProviderUserKey,
                        Author = Request.Form["author"],
                        QuoteText = Request.Form["quoteText"],
                        Approved = false                        
                    };

                    if (Roles.IsUserInRole("Administrator"))
                        quote.Approved = true;

                    db.Quotes.InsertOnSubmit(quote);

                    db.SubmitChanges();

                    return RedirectToAction("Index", "Quotes");
                }
            }
            catch
            {
                return RedirectToAction("Error", "Updates");
            }
        }

        public ActionResult Edit(int id)
        {
            try
            {
                using (InOutDataContext db = new InOutDataContext())
                {

                    return View();
                }
            }
            catch
            {
                return RedirectToAction("Error", "Updates");
            }
        }

        public ActionResult Update(int id)
        {
            try
            {
                using (InOutDataContext db = new InOutDataContext())
                {

                    return View();
                }
            }
            catch
            {
                return RedirectToAction("Error", "Updates");
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                using (InOutDataContext db = new InOutDataContext())
                {

                    return View();
                }
            }
            catch
            {
                return RedirectToAction("Error", "Updates");
            }
        }

        public ActionResult Destroy(int id)
        {
            try
            {
                using (InOutDataContext db = new InOutDataContext())
                {

                    return View();
                }
            }
            catch
            {
                return RedirectToAction("Error", "Updates");
            }
        }
    }
}
